package com.angis.fx.activity.draft;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

import com.angis.fx.activity.Enforcement;
import com.angis.fx.activity.R;
import com.angis.fx.activity.jcdj.MultipleDialogActivity;
import com.angis.fx.activity.login.LoginActivity;
import com.angis.fx.activity.search.ChangsuoPictureActivity;
import com.angis.fx.activity.service.CheckDutyService;
import com.angis.fx.activity.sxry.SxryActivity;
import com.angis.fx.activity.sxry.SxryMActivity;
import com.angis.fx.activity.zdcf.AttendanceActivity;
import com.angis.fx.adapter.MultipleAdapter;
import com.angis.fx.adapter.MultipleAdapter.ViewHolder;
import com.angis.fx.data.ChangsuoInformation;
import com.angis.fx.data.DraftInfo;
import com.angis.fx.db.CheckDraftDBHelper;
import com.angis.fx.db.QuickTextDBHelper;
import com.angis.fx.db.ZCTypeDBHelper;
import com.angis.fx.util.CommonUtil;
import com.angis.fx.util.ContentValuesUtil;

public class DetailTodayCheckActivity extends Activity {

	private EditText mPlacenameEt;
	private Spinner mPlaceTypeSp;
	private Spinner mAreaTypeSp;
	private EditText mPermitEt;
	private EditText mPrincipalEt;
	private EditText mAddressEt;
	private Spinner mCheckResultSp;
	private EditText mQuickTextEt;
	private Spinner mCheckSituationSp;
	private EditText mArrivalDateEt;
	private EditText mLeaveDateEt;

	private Button mPlacenameBt;
	private Button mQuickTextBt;
	private Button mArrivalDateBt;
	private Button mLeaveDateBt;
	private Button mEditBt;
	private Button mReturnBt;
	private Button dcr_upload_pic_bt;

	private ChangsuoInformation mCsInfo;
	private MultipleAdapter mMultipleDialogListAdapter;
	private CheckedTextView mCheckTextView;
	private Set<Integer> mPos;
	private Intent mIntent;
	private String mId;
	//private UpdateReceiver mUpdateReceiver;
	private boolean isSave = false;
	private UpdateReceiver receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dailycheckresultactivity);
		mCsInfo = (ChangsuoInformation) getIntent().getExtras().get(
				"selectedcs");
		if (mCsInfo == null) {
			mId = getIntent().getExtras().get("draftId").toString();
			CheckDraftDBHelper helper = new CheckDraftDBHelper(this);
			DraftInfo info = helper.findById(mId);
			helper.closeDB();

			mCsInfo = new ChangsuoInformation();
			mCsInfo.setAddress(info.getAddress());
			mCsInfo.setArea(info.getArea());
			mCsInfo.setAreaCode(info.getAreacode());
			mCsInfo.setAreaType(info.getAreatype());
			mCsInfo.setCode(info.getCode());
			mCsInfo.setCsId(info.getCsid());
			mCsInfo.setHyCode(info.getHycode());
			mCsInfo.setLat(Double.parseDouble(info.getY()));
			mCsInfo.setLon(Double.parseDouble(info.getX()));
			mCsInfo.setManagePos(info.getManagepos());
			mCsInfo.setPersonInCharge(info.getManager());
			mCsInfo.setPermitCode(info.getPermitcode());
			mCsInfo.setPermitWord(info.getPermitword());
			mCsInfo.setTelefon(info.getTel());
			mCsInfo.setTitle(info.getContitle());
			mCsInfo.setUnitType(info.getUnittype());
			mCsInfo.setJcjg(info.getJcjg());
			mCsInfo.setJcqk(info.getJcqk());
			mCsInfo.setCheckxs(info.getCheckxs());
			mCsInfo.setArrivaltime(info.getArrivaltime());
			mCsInfo.setLeavetime(info.getLeavetime());
			mCsInfo.setDatastatus(info.getDatastatus());
			if (info.getXlongps() != null && !info.getXlongps().equals("")) {
				mCsInfo.setXlonGPS(Double.parseDouble(info.getXlongps()));
				mCsInfo.setYlatGPS(Double.parseDouble(info.getYlatgps()));
			}
		}
		initActivity();

		setDefaultValue();

		// mPlacenameBt.setVisibility(View.GONE);
		// mDetailBt.setVisibility(View.GONE);
		// mQuickTextBt.setVisibility(View.GONE);
		// mArrivalDateBt.setEnabled(false);
		// mLeaveDateBt.setEnabled(false);

		mQuickTextBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mIntent = new Intent();
				mPos = new HashSet<Integer>();
				LayoutInflater layoutInflater = LayoutInflater
						.from(DetailTodayCheckActivity.this);
				final View lView = layoutInflater.inflate(
						R.layout.multiple_dialog, null);
				ListView mListView = (ListView) lView
						.findViewById(R.id.multiple_dialoglist);
				mMultipleDialogListAdapter = new MultipleAdapter(
						DetailTodayCheckActivity.this);
				List<String> lDataList = new ArrayList<String>();
				QuickTextDBHelper lDBHelper = new QuickTextDBHelper(
						DetailTodayCheckActivity.this);
				Cursor lCursor = lDBHelper.getAllQuickText();
				if (lCursor.moveToFirst()) {
					do {
						lDataList.add(lCursor.getString(1));
					} while (lCursor.moveToNext());
				}
				lCursor.close();
				lDBHelper.closeDB();
				initMultipleDialog(mListView, lDataList);
				new AlertDialog.Builder(DetailTodayCheckActivity.this).setTitle(
						"快速文本").setView(lView).setPositiveButton(
						R.string.confirm,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								StringBuffer lSb = new StringBuffer();
								for (Integer pos : mPos) {
									ViewHolder lHodler = (ViewHolder) mMultipleDialogListAdapter
											.getView(pos, null, null).getTag();
									lSb.append(
											lHodler.getName().getText()
													.toString()).append(";");
								}
								mQuickTextEt.setText(mQuickTextEt.getText()
										.toString()
										+ lSb.toString());
							}
						}).setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								dialog.cancel();
							}
						}).create().show();
			}
		});
		mEditBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				CheckDraftDBHelper lDBHelper = new CheckDraftDBHelper(
						DetailTodayCheckActivity.this);
				if (mId != null && !mId.equals("")) {
					try {
						DraftInfo lDraftInfo = initCheckDraft();
						lDBHelper.updateDraft(lDraftInfo);
						Toast.makeText(DetailTodayCheckActivity.this, "更新草稿成功!",
								Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(DetailTodayCheckActivity.this, "更新草稿失败!",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					try {
						DraftInfo lDraftInfo = initCheckDraft();
						ContentValues lValues = ContentValuesUtil
								.convertCheckDraft(lDraftInfo);
						lDBHelper.insert(lValues);

						Toast.makeText(DetailTodayCheckActivity.this, "保存草稿成功!",
								Toast.LENGTH_SHORT).show();
					} catch (Exception e) {
						Toast.makeText(DetailTodayCheckActivity.this, "保存草稿失败!",
								Toast.LENGTH_SHORT).show();
					}
				}
				lDBHelper.closeDB();
			}
		});
		mReturnBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				receiver = new UpdateReceiver();
				IntentFilter filter = new IntentFilter();
				// find a action. com.hoperun.gpsService is defined in
				// GpsService class
				filter.addAction("upLoadResult");
				DetailTodayCheckActivity.this.registerReceiver(receiver, filter);

				mIntent = new Intent(DetailTodayCheckActivity.this,
						CheckDutyService.class);
				Bundle lBundle = new Bundle();
				DraftInfo lDraftInfo = initCheckDraft();
				lBundle.putSerializable("CHECKDUTY", lDraftInfo);
				lBundle.putBoolean("isSave", isSave);
				mIntent.putExtras(lBundle);
				startService(mIntent);
				// DailyCheckResultActivity.this.
			}
		});
		mArrivalDateBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mIntent = new Intent();
				Bundle lBundle = new Bundle();
				lBundle.putInt("RESULTCODE", 1);
				mIntent.putExtras(lBundle);
				mIntent.setClass(DetailTodayCheckActivity.this,
						AttendanceActivity.class);
				DetailTodayCheckActivity.this.startActivityForResult(mIntent, 1);
			}
		});
		mLeaveDateBt.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mIntent = new Intent();
				Bundle lBundle = new Bundle();
				lBundle.putInt("RESULTCODE", 2);
				mIntent.putExtras(lBundle);
				mIntent.setClass(DetailTodayCheckActivity.this,
						AttendanceActivity.class);
				DetailTodayCheckActivity.this.startActivityForResult(mIntent, 1);
			}
		});
		mCheckResultSp.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				String lSelected = ((TextView)mCheckResultSp.getSelectedView()).getText().toString();
				if("正常" != lSelected) {
					mPos = new HashSet<Integer>();
					List<String> lDataList = new ArrayList<String>();
					if(!"其他".equals(lSelected)) {
						Bundle lBundle = new Bundle();
						Intent lIntent = new Intent();
						lBundle.putInt("RESULTCODE", 3);
						lBundle.putString("type", ((TextView)mPlaceTypeSp.getSelectedView()).getText().toString());
						lIntent.putExtras(lBundle);
						lIntent.setClass(DetailTodayCheckActivity.this, MultipleDialogActivity.class);
						DetailTodayCheckActivity.this.startActivityForResult(lIntent, lBundle.getInt("RESULTCODE"));
					}else {
						mMultipleDialogListAdapter = new MultipleAdapter(DetailTodayCheckActivity.this);
						LayoutInflater lLayoutInflater = LayoutInflater.from(DetailTodayCheckActivity.this);
						View lView = lLayoutInflater.inflate(R.layout.multiple_dialog, null);
						ListView mListView = (ListView)lView.findViewById(R.id.multiple_dialoglist);
						ZCTypeDBHelper lDBHelper2 = new ZCTypeDBHelper(DetailTodayCheckActivity.this);
						Cursor lCursor = lDBHelper2.getZCTypeByConditionType(((TextView)mPlaceTypeSp.getSelectedView()).getText().toString());
						if(lCursor.moveToFirst()) {
							do {
								String type = lCursor.getString(2);
								lDataList.add(type);
							} while (lCursor.moveToNext());
						}
						lCursor.close();
						lDBHelper2.closeDB();
						initMultipleDialog(mListView, lDataList);
						new AlertDialog.Builder(DetailTodayCheckActivity.this)
							.setTitle(lSelected)
							.setView(lView)
							.setPositiveButton(R.string.confirm,
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int whichButton) {
											StringBuffer lSb = new StringBuffer();
											for (Integer pos : mPos) {
												ViewHolder lHodler = (ViewHolder) mMultipleDialogListAdapter.getView(pos, null, null).getTag();
												lSb.append(lHodler.getName().getText().toString()).append(";");
											}
											mQuickTextEt.setText(lSb.toString());
										}
									})
							.setNegativeButton(R.string.cancel,
									new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog,
												int whichButton) {
											dialog.cancel();
										}
								}).create().show();
					}
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				mCheckResultSp.setSelection(0);
			}
		});
//		mCheckResultSp.setOnItemSelectedListener(new OnItemSelectedListener() {
//
//			@Override
//			public void onItemSelected(AdapterView<?> arg0, View arg1,
//					int arg2, long arg3) {
//				String lSelected = ((TextView) mCheckResultSp.getSelectedView())
//						.getText().toString();
//				if ("正常" != lSelected) {
//					lPos = new HashSet<Integer>();
//					mMultipleDialogListAdapter = new MultipleAdapter(
//							DetailTodayCheckActivity.this);
//					LayoutInflater lLayoutInflater = LayoutInflater
//							.from(DetailTodayCheckActivity.this);
//					View lView = lLayoutInflater.inflate(
//							R.layout.multiple_dialog, null);
//					ListView mListView = (ListView) lView
//							.findViewById(R.id.multiple_dialoglist);
//					List<String> lDataList = new ArrayList<String>();
//					if (!"其他".equals(lSelected)) {
//						WGTypeDBHelper lDBHelper = new WGTypeDBHelper(
//								DetailTodayCheckActivity.this);
//						Cursor lCursor = lDBHelper
//								.getWGTypeByConditionType(mCsInfo.getAreaType());
//						if (lCursor.moveToFirst()) {
//							do {
//								String type = lCursor.getString(2);
//								lDataList.add(type);
//							} while (lCursor.moveToNext());
//						}
//						lCursor.close();
//						lDBHelper.closeDB();
//					} else {
//						ZCTypeDBHelper lDBHelper2 = new ZCTypeDBHelper(
//								DetailTodayCheckActivity.this);
//						Cursor lCursor = lDBHelper2
//								.getZCTypeByConditionType("其它");
//						if (lCursor.moveToFirst()) {
//							do {
//								String type = lCursor.getString(2);
//								lDataList.add(type);
//							} while (lCursor.moveToNext());
//						}
//						lCursor.close();
//						lDBHelper2.closeDB();
//					}
//					initMultipleDialog(mListView, lDataList);
//					new AlertDialog.Builder(DetailTodayCheckActivity.this).setTitle(
//							lSelected).setView(lView).setPositiveButton(
//							R.string.confirm,
//							new DialogInterface.OnClickListener() {
//								public void onClick(DialogInterface dialog,
//										int whichButton) {
//									StringBuffer lSb = new StringBuffer();
//									for (Integer pos : lPos) {
//										ViewHolder lHodler = (ViewHolder) mMultipleDialogListAdapter
//												.getView(pos, null, null)
//												.getTag();
//										lSb.append(
//												lHodler.getName().getText()
//														.toString())
//												.append(",");
//									}
//								}
//							}).setNegativeButton(R.string.cancel,
//							new DialogInterface.OnClickListener() {
//								public void onClick(DialogInterface dialog,
//										int whichButton) {
//									dialog.cancel();
//								}
//							}).create().show();
//				}
//			}
//
//			@Override
//			public void onNothingSelected(AdapterView<?> arg0) {
//				//mCheckResultSp.setSelection(0);
//			}
//
//		});

		dcr_upload_pic_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(DetailTodayCheckActivity.this,
						ChangsuoPictureActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("pictype", "2");
				bundle.putString("recordid", mId);
				bundle.putString("picrecordid", mCsInfo.getJcjg());
				intent.putExtras(bundle);
				DetailTodayCheckActivity.this.startActivity(intent);
			}
		});
	}

	// public class UpdateReceiver extends BroadcastReceiver{
	// @Override
	// public void onReceive(Context context, Intent intent) {
	// String response = intent.getStringExtra("uploadresult");
	// if("0".equals(response)) {
	// Toast.makeText(DailyCheckResultActivity.this, "上传失败!",
	// Toast.LENGTH_SHORT).show();
	// }else if("-1".equals(response)) {
	// Toast.makeText(DailyCheckResultActivity.this, "服务器连接错误!",
	// Toast.LENGTH_SHORT).show();
	// }else if("1".equals(response)) {
	// mReturnBt.setEnabled(false);
	// Toast.makeText(DailyCheckResultActivity.this, "上传成功!",
	// Toast.LENGTH_SHORT).show();
	// }
	// }
	//		
	// }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (null != data && null != data.getExtras()) {
			Bundle lBundle = data.getExtras();
			String lDatetime = lBundle.getString("DATATIME");

			String jcjg = lBundle.getString("JCJG");
			SimpleDateFormat lSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			switch (resultCode) {
			case 1:  
	        	try {
	        		String tempDatetime = mArrivalDateEt.getText().toString();
	        		mArrivalDateEt.setText(lDatetime);
	        		if(lSdf.parse(mArrivalDateEt.getText().toString())
	        				.compareTo(lSdf.parse(lSdf.format(Enforcement.mStartdutytime))) < 0){
	        			mArrivalDateEt.setText(tempDatetime);
	        			Toast.makeText(DetailTodayCheckActivity.this, "到达时间比开始时间早,请重新选择!", Toast.LENGTH_LONG).show();
	        			return;
	        		}
				} catch (Exception e) {
				}
	            break;
	        case 2:
	        	try {
	        		String tempDatetime = mLeaveDateEt.getText().toString();
	        		mLeaveDateEt.setText(lDatetime);
	        		if(mArrivalDateEt.getText().toString().length() > 0) {
		        		if(lSdf.parse(mArrivalDateEt.getText().toString()).compareTo(lSdf.parse(lDatetime)) > 0) {
		        			mLeaveDateEt.setText(tempDatetime);
		        			Toast.makeText(DetailTodayCheckActivity.this, "离开时间比到达时间早,请重新选择!", Toast.LENGTH_LONG).show();
		        		}else {
		        			mLeaveDateEt.setText(lDatetime);
		        		}
	        		}else {
	        			mLeaveDateEt.setText(tempDatetime);
	        			Toast.makeText(DetailTodayCheckActivity.this, "请先选择到达时间!", Toast.LENGTH_LONG).show();
	        		}
				} catch (Exception e) {
				}
	        	break;
			case 3:
				mQuickTextEt.setText(jcjg);
				break;
			}
		}
	}

	private DraftInfo initCheckDraft() {
		if (mQuickTextEt.getText().toString().length() < 1) {
			Toast.makeText(DetailTodayCheckActivity.this, "检查情况不能为空!",
					Toast.LENGTH_SHORT).show();
			return null;
		}
		if (mArrivalDateEt.getText().toString().length() < 1) {
			Toast.makeText(DetailTodayCheckActivity.this, "到达时间不能为空!",
					Toast.LENGTH_SHORT).show();
			return null;
		}
		if (mLeaveDateEt.getText().toString().length() < 1) {
			Toast.makeText(DetailTodayCheckActivity.this, "离开时间不能为空!",
					Toast.LENGTH_SHORT).show();
			return null;
		}
		if (mId == null || mId.equals(""))
			mId = UUID.randomUUID().toString();
		DraftInfo lDraftInfo = new DraftInfo(
				mId,
				mCsInfo.getCsId(),
				mCsInfo.getTitle(),
				mCsInfo.getAddress(),
				mCsInfo.getPersonInCharge(),
				mCsInfo.getAreaType(),
				mCsInfo.getPermitCode(),
				mCsInfo.getPermitWord(),
				mCsInfo.getArea(),
				mCsInfo.getAreaCode(),
				mCsInfo.getHyCode(),
				mCsInfo.getCode(),
				mCsInfo.getManagePos(),
				mCsInfo.getTelefon(),
				mCsInfo.getUnitType(),
				getCurrentJcjp(),
				CommonUtil.getCurrentDate(),
				mQuickTextEt.getText().toString(),
				"",
				SxryMActivity.SXRP_SELECT_MEMBER.replaceAll(",", "|"),
				SxryActivity.QTRY_EDIT_MEMBER,
				CommonUtil.randomNumber(19),
				CommonUtil.randomUUID(),
				SxryMActivity.SXRP_SELECT_MEMBER.split(",").length + "",
				LoginActivity.g_username,
				mArrivalDateEt.getText().toString(),
				mLeaveDateEt.getText().toString(),
				Enforcement.mDutyno,
				getDjguid(),
				((TextView) mCheckSituationSp.getSelectedView()).getText()
						.toString(),
				(SxryMActivity.SXRP_SELECT_MEMBER.split(",").length + SxryActivity.QTRY_EDIT_MEMBER_COUNT)
						+ "", mCsInfo.getArea(), "0", "", String
						.valueOf(mCsInfo.getLon()), String.valueOf(mCsInfo
						.getLat()), "", "", getCheckTime());
		return lDraftInfo;
	}

	private String getDjguid() {
		String lDjguid = ((TextView) mCheckSituationSp.getSelectedView())
				.getText().toString();
		if ("明察".equals(lDjguid)) {
			return Enforcement.mMcTJGuid;
		} else {
			return Enforcement.mAfTJGuid;
		}
	}

	private String getCheckTime() {
		try {
			String date1 = mArrivalDateEt.getText().toString();
			String date2 = mLeaveDateEt.getText().toString();
			return date1.split(" ")[1].substring(0,date1.split(" ")[1].lastIndexOf(":")) + "-" + date2.split(" ")[1].substring(0,date2.split(" ")[1].lastIndexOf(":"));
		} catch (Exception e) {
			return "";
		}
	}

	private String getCurrentJcjp() {
		String lStr = ((TextView) mCheckResultSp.getSelectedView()).getText()
				.toString();
		if ("正常".equals(lStr)) {
			return "0";
		} else if ("其他".equals(lStr)) {
			return "1";
		} else if ("违规".equals(lStr)) {
			return "2";
		} else {
			return "0";
		}
	}

	private void initMultipleDialog(ListView mListView, List<String> lDataList) {
		mMultipleDialogListAdapter.setData(lDataList,mPos);
		mListView.setAdapter(mMultipleDialogListAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mCheckTextView = (CheckedTextView) view
						.findViewById(R.id.multiple_dialog_item_ctv);
				if (mCheckTextView.isChecked()) {
					mPos.remove(position);
					mCheckTextView.setChecked(false);
				} else if (!mCheckTextView.isChecked()) {
					mPos.add(position);
					mCheckTextView.setChecked(true);
				}
			}
		});
	}

	private void setDefaultValue() {
		mPlacenameEt.setText(mCsInfo.getTitle());
		mPlacenameEt.setEnabled(false);
		mAddressEt.setText(mCsInfo.getAddress());
		mAddressEt.setEnabled(false);
		mPermitEt.setText(mCsInfo.getPermitCode());
		mPermitEt.setEnabled(false);
		mPrincipalEt.setText(mCsInfo.getPersonInCharge());
		mPrincipalEt.setEnabled(false);
		mArrivalDateEt.setText("");
		mArrivalDateEt.setEnabled(false);
		mLeaveDateEt.setText("");
		mLeaveDateEt.setEnabled(false);
		dcr_upload_pic_bt.setEnabled(false);

		ArrayAdapter<String> lAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		lAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lAdapter.add("正常");
		lAdapter.add("其他");
		lAdapter.add("违规");
		mCheckResultSp.setAdapter(lAdapter);
		lAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		lAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lAdapter.add("明查");
		lAdapter.add("暗访");
		mCheckSituationSp.setAdapter(lAdapter);

		lAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item);
		lAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lAdapter.add(mCsInfo.getAreaType());
		mPlaceTypeSp.setAdapter(lAdapter);
		mPlaceTypeSp.setEnabled(false);
		mPlacenameBt.setEnabled(false);

		if (mCsInfo.getJcqk() != null && !mCsInfo.getJcqk().equals("")) {
			mQuickTextEt.setText(mCsInfo.getJcqk());
			mArrivalDateEt.setText(mCsInfo.getArrivaltime());
			mLeaveDateEt.setText(mCsInfo.getLeavetime());
			mCheckResultSp.setSelection(Integer.parseInt(mCsInfo.getJcjg()));
			if ("明查".equals(mCsInfo.getCheckxs())) {
				mCheckSituationSp.setSelection(0);
			} else {
				mCheckSituationSp.setSelection(1);
			}
			if (mCsInfo.getDatastatus().equals("1")) {
				dcr_upload_pic_bt.setEnabled(true);
				mReturnBt.setEnabled(false);
			}
			isSave = true;
			mEditBt.setText("更新草稿");
		}
		lAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
		lAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lAdapter.add(mCsInfo.getArea());
		mAreaTypeSp.setAdapter(lAdapter);
		mAreaTypeSp.setEnabled(false);
		mAreaTypeSp.setFocusable(false);
	}

	private void initActivity() {
		mPlacenameEt = (EditText) findViewById(R.id.dcr_placename_et);
		mPlacenameEt.setEnabled(false);
		mPlaceTypeSp = (Spinner) findViewById(R.id.dcr_placetype_sp);
		mPlaceTypeSp.setEnabled(false);
		mAreaTypeSp = (Spinner) findViewById(R.id.dcr_areatype_sp);
		mPermitEt = (EditText) findViewById(R.id.dcr_permit_et);
		mPermitEt.setEnabled(false);
		mPrincipalEt = (EditText) findViewById(R.id.dcr_principal_et);
		mPrincipalEt.setEnabled(false);
		mAddressEt = (EditText) findViewById(R.id.dcr_address_et);
		mAddressEt.setEnabled(false);
		mQuickTextEt = (EditText) findViewById(R.id.dcr_quicktext_et);
		mQuickTextEt.setEnabled(false);
		mCheckResultSp = (Spinner) findViewById(R.id.dcr_checkresult_sp);
		mCheckResultSp.setEnabled(false);
		mCheckSituationSp = (Spinner) findViewById(R.id.dcr_checksituation_sp);
		mCheckSituationSp.setEnabled(false);
		mArrivalDateEt = (EditText) findViewById(R.id.dcr_arrivaldate_tx);
		mArrivalDateEt.setEnabled(false);
		mLeaveDateEt = (EditText) findViewById(R.id.dcr_leavedate_tx);
		mLeaveDateEt.setEnabled(false);
		mPlacenameBt = (Button) findViewById(R.id.dcr_placename_bt);
		mPlacenameBt.setEnabled(false);
		mQuickTextBt = (Button) findViewById(R.id.dcr_quicktext_bt);
		mQuickTextBt.setEnabled(false);
		mArrivalDateBt = (Button) findViewById(R.id.dcr_arrival_bt);
		mArrivalDateBt.setEnabled(false);
		mLeaveDateBt = (Button) findViewById(R.id.dcr_leave_bt);
		mLeaveDateBt.setEnabled(false);
		mEditBt = (Button) findViewById(R.id.dcr_savedraft_bt);
		mEditBt.setEnabled(false);
		mReturnBt = (Button) findViewById(R.id.dcr_upload_bt);
		mReturnBt.setEnabled(false);
		dcr_upload_pic_bt = (Button) findViewById(R.id.dcr_upload_pic_bt);
	}

	public class UpdateReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			DetailTodayCheckActivity.this.unregisterReceiver(receiver);
			boolean isCompleteUpload = intent.getBooleanExtra(
					"isCompleteUpload", false);
			if (isCompleteUpload) {
				DetailTodayCheckActivity.this.mEditBt.setEnabled(false);
				DetailTodayCheckActivity.this.mReturnBt.setEnabled(false);
				DetailTodayCheckActivity.this.dcr_upload_pic_bt.setEnabled(true);
			}
		}
	}

}
